package com.yun.zf.app.service.impl;

import com.yun.zf.common.admin.domain.LoginUser;
import com.yun.zf.common.app.model.wechat.WeChatUserInfoMo;
import com.yun.zf.system.exception.CustomException;
import com.yun.zf.system.security.service.TokenService;
import com.yun.zf.system.security.token.WxAppletAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PersonCenterService  {
    @Resource
    private AuthenticationManager authenticationManager;
    @Autowired
    private TokenService tokenService;

    public String wxLogin(WeChatUserInfoMo weChatUserInfoMo){
        // 用户验证
        Authentication authentication = null;
        try
        {
            authentication = authenticationManager
                    .authenticate(new WxAppletAuthenticationToken(weChatUserInfoMo));
        }
        catch (Exception e)
        {
            throw new CustomException(e.getMessage());
        }

        System.out.println(authentication.getPrincipal());
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return tokenService.createToken(null);
    }
}
